import { createApp } from 'vue'
import { createPinia } from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'  // 添加这行
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import './style.css'
import App from './App.vue'
import router from './router/index.js'
import axios from 'axios'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import { ElMessage } from 'element-plus'

const app = createApp(App)
const pinia = createPinia()

axios.defaults.baseURL = 'http://localhost:8080'
// 使用持久化插件
pinia.use(piniaPluginPersistedstate)  // 添加这行

// 确保 pinia 状态恢复后再挂载应用
const init = async () => {
  app.use(pinia)
  
  // 等待 pinia 持久化数据恢复
  await Promise.resolve(pinia._p?.[0]?.restored || Promise.resolve())
  
  app.use(ElementPlus, { locale: zhCn })
  app.use(router)
  app.config.globalProperties.$axios = axios
  app.config.globalProperties.$http = axios
  app.config.globalProperties.$message = ElMessage
  app.mount('#app')
}

init()